package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.com.kunpeng.entity.OaStorehouseInfo;
import org.jeecg.modules.demo.com.kunpeng.entity.OaStorehouseDetail;
import org.jeecg.modules.demo.com.kunpeng.mapper.OaStorehouseDetailMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.OaStorehouseInfoMapper;
import org.jeecg.modules.demo.com.kunpeng.service.IOaStorehouseInfoService;
import org.jeecg.modules.demo.com.kunpeng.vo.OaStorehouseDetailQueryListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 仓库表
 * @Author: jeecg-boot
 * @Date:   2023-02-28
 * @Version: V1.0
 */
@Service
public class OaStorehouseInfoServiceImpl extends ServiceImpl<OaStorehouseInfoMapper, OaStorehouseInfo> implements IOaStorehouseInfoService {

	@Autowired
	private OaStorehouseInfoMapper oaStorehouseInfoMapper;
	@Autowired
	private OaStorehouseDetailMapper oaStorehouseDetailMapper;

	/**
	 * 新增
	 * @param oaStorehouseInfo
	 * @param oaStorehouseDetailList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(OaStorehouseInfo oaStorehouseInfo, List<OaStorehouseDetail> oaStorehouseDetailList) {
		oaStorehouseInfoMapper.insert(oaStorehouseInfo);
		if(oaStorehouseDetailList!=null && oaStorehouseDetailList.size()>0) {
			for(OaStorehouseDetail entity:oaStorehouseDetailList) {
				//外键设置
				entity.setOsid(oaStorehouseInfo.getId());
				oaStorehouseDetailMapper.insert(entity);
			}
		}
	}

	/**
	 * 修改
	 * @param oaStorehouseInfo
	 * @param oaStorehouseDetailList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(OaStorehouseInfo oaStorehouseInfo,List<OaStorehouseDetail> oaStorehouseDetailList) {
		oaStorehouseInfoMapper.updateById(oaStorehouseInfo);
		
		//1.先删除子表数据
		oaStorehouseDetailMapper.deleteByMainId(oaStorehouseInfo.getId());
		
		//2.子表数据重新插入
		if(oaStorehouseDetailList!=null && oaStorehouseDetailList.size()>0) {
			for(OaStorehouseDetail entity:oaStorehouseDetailList) {
				//外键设置
				entity.setOsid(oaStorehouseInfo.getId());
				oaStorehouseDetailMapper.insert(entity);
			}
		}
	}

	/**
	 * 根据id删除
	 * @param id
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		oaStorehouseDetailMapper.deleteByMainId(id);
		oaStorehouseInfoMapper.deleteById(id);
	}

	/**
	 * 批量删除
	 * @param idList
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			oaStorehouseDetailMapper.deleteByMainId(id.toString());
			oaStorehouseInfoMapper.deleteById(id);
		}
	}

	/**
	 * 根据id修改仓库状态
	 * @param id
	 */
	@Override
	public String updateStorehouseStateById(String id) {
		OaStorehouseInfo oaStorehouseInfo = oaStorehouseInfoMapper.selectById(id);
		if(oaStorehouseInfo.getState()==0){
			oaStorehouseInfoMapper.updateStorehouseStateById(id,1);
			return "禁用成功！";
		}else if(oaStorehouseInfo.getState()==1){
			oaStorehouseInfoMapper.updateStorehouseStateById(id,0);
			return "启用成功！";
		}
		return "修改失败,请联系管理员！";
	}

	/**
	 * 根据仓库名称模糊查询
	 * @param name
	 * @return
	 */
	@Override
	public Page<OaStorehouseDetailQueryListVO> oaStorehouseInfoListByName(Page<OaStorehouseInfo>page, String name) {
		//新建返回值page集合
		Page<OaStorehouseDetailQueryListVO> pageInfo=new Page<>();
		//获取所有仓库列表
		List<OaStorehouseInfo> oaStorehouseInfos = oaStorehouseInfoMapper.oaStorehouseInfoListByName(page, name);
		List<OaStorehouseDetailQueryListVO> oaStorehouseDetailQueryListVOList=new ArrayList<>();
		if(oaStorehouseInfos.size()>0){
			for(OaStorehouseInfo oaStorehouseInfo:oaStorehouseInfos){
				//获取仓库库位列表
				List<OaStorehouseDetail> oaStorehouseDetails = oaStorehouseDetailMapper.selectByMainId(oaStorehouseInfo.getId());
				if(oaStorehouseDetails.size()>0){
					//给每一个返回值赋值
					for(OaStorehouseDetail oaStorehouseDetail:oaStorehouseDetails){
						OaStorehouseDetailQueryListVO oaStorehouseDetailQueryListVO=new OaStorehouseDetailQueryListVO();
						oaStorehouseDetailQueryListVO.setName(oaStorehouseInfo.getName());
						oaStorehouseDetailQueryListVO.setShelvesNum(oaStorehouseDetail.getShelvesNum());
						oaStorehouseDetailQueryListVO.setWarehouseLocation(oaStorehouseDetail.getWarehouseLocation());
						oaStorehouseDetailQueryListVO.setId(oaStorehouseDetail.getId());
						oaStorehouseDetailQueryListVOList.add(oaStorehouseDetailQueryListVO);
					}
				}
			}
		}
		pageInfo.setRecords(oaStorehouseDetailQueryListVOList);
		return pageInfo;
	}
}
